/**
 * @(#) CongMoneyMediator.as
 *
 * @date		2009. 03. 25.
 * @version		1.0
 * @author		baek, kwang hyoun
 * @description	건강보험증 신청 - Mediator
 * 				건강보험증을 조회하고 신청하는 Presentation Logic을 관리
 */
package com.skt.common_hr.appmss.view
{
	import com.skt.common_hr.appmss.AppmssFacade;
	import com.skt.common_hr.appmss.events.SelMemberEvent;
	import com.skt.common_hr.appmss.events.SelOrgEvent;
	import com.skt.common_hr.appmss.model.MemberSearchProxy;
	import com.skt.common_hr.appmss.model.OrgProxy;
	import com.skt.common_hr.appmss.model.vo.MemberVO;
	import com.skt.common_hr.appmss.view.components.AppmssOverViewSac;
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.OverViewMediator;
	
	import mx.collections.ArrayCollection;
	
	import org.puremvc.as3.multicore.interfaces.INotification;

	public class AppmssMediator extends OverViewMediator
	{
		public static const NAME:String = "MedicineMediator";
		private var selectedMember:MemberVO;

		/**
		* 생성자
		*
		* @param
		* @return void
		*/
        public function AppmssMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }

		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		* 최초 한번만 불림
		*/
 		override protected function initializeView():void
 		{
 			preloadData();
			super.initializeView();
 		}
		/***
		 * 활성화가 될때마다 불림.
		 */
		override protected function activatedView():void
		{
			super.activatedView();		
			view.loadMainList('');
			view.lbSelectedName.text = '';


			setEventListener();
						
			loadTeamMember(Variables.myOrgCode);			
		}

		override protected function closedView():void
		{
			removeEventListener();
		}

 		private function setEventListener():void
 		{
 			view.addEventListener(SelMemberEvent.SELECTED_MEMBER, onSelectedMember);
 			view.addEventListener(SelOrgEvent.SELECTED_ORG, onSelectedOrg); 			 			
 		}

 		private function removeEventListener():void
 		{
 			view.removeEventListener(SelMemberEvent.SELECTED_MEMBER, onSelectedMember);
 			view.removeEventListener(SelOrgEvent.SELECTED_ORG, onSelectedOrg);			
 		}

		private function preloadData():void
 		{
 			var orgProxy:OrgProxy = facade.retrieveProxy(OrgProxy.NAME) as OrgProxy;
 			orgProxy.load(Variables.myPernr, "X");
 			
// 			var cdpProxy:OrgProxy = facade.retrieveProxy(OrgProxy.NAME) as OrgProxy;
// 			cdpProxy.cdpLoad(Variables.myPernr);

 			view.txtOrg.text = Variables.myOrg;
 			view.txtOrg.data = Variables.myOrgCode;
 			 			
 			//view.loadMainList();
 		}

 		private function onSelectedMember(event:SelMemberEvent):void
 		{
			 selectedMember = event.member;
			 view.loadMainList(selectedMember.PERNR);


// 			var cdpProxy:OrgProxy = facade.retrieveProxy(OrgProxy.NAME) as OrgProxy;
// 			cdpProxy.cdpLoad(selectedMember.PERNR);
 			
			view.lbSelectedName.text = selectedMember.ENAME + '(' + selectedMember.PERNR + ')';
 		}

 		private function onSelectedMemberSearch(event:SelMemberEvent):void
 		{
			 //selectedMember = event.member;
			 loadTeamMember("event.orgCode");
 		}


 		private function onSelectedOrg(event:SelOrgEvent):void
 		{
			loadTeamMember(event.orgCode); 			
 		} 		
 		 		

 		private function loadTeamMember(code:String):void
 		{
 			var proxy:MemberSearchProxy = facade.retrieveProxy(MemberSearchProxy.NAME) as MemberSearchProxy;
 			proxy.load(code, "");
 		}

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		*
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array
		{
			return basicInterestMessages.concat([
					// reload
						AppmssFacade.LOAD_ORGLIST_COMPLETE,
						AppmssFacade.LOAD_ORGLIST_FAILED,
						AppmssFacade.LOAD_NAMESEARCH_COMPLETE,
						AppmssFacade.LOAD_NAMESEARCH_FAILED
					]);
		}


		/**
		* Noti별로 처리한다.
		*
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			super.handleNotification(note);
			switch(note.getName())
			{
				// 의료비 리스트  조회 Proxy 호출결과 처리
				case AppmssFacade.LOAD_NAMESEARCH_COMPLETE:
					view.memberList = (note.getBody() as MemberSearchProxy).memberList;
					commonFacade.setStatusMessage("구성원 사진을 클릭하시면 조회하실 수 있습니다."); 
					break;
				case AppmssFacade.LOAD_ORGLIST_COMPLETE:
					view.orgList = (note.getBody() as OrgProxy).orgList;
					view.cdpList = (note.getBody() as OrgProxy).cdpList;
					break;
				case AppmssFacade.LOAD_ORGLIST_FAILED:
					break;					
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}


 		/**
		 * Proxy에서 DataLoad를 완료한 후 호출됨
		 *     - load된 데이터를 화면에 출력
		 * @param
		 * @return void
		 */
 		private function setListView(list:ArrayCollection):void
 		{
// 			view.medicalMoneyDataList = list;
// 			commonFacade.setStatusMessage("STATUS_LOADED_LIST", [list.length.toString()]);
// 			if(list.length == 0) {
// 				view.showInitialTip();
// 			}
 		}


		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event handler (from View, Internal or....)
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
       private function get view():AppmssOverViewSac
        {
            return viewComponent as AppmssOverViewSac;
        }


	}
}